package dev;


import jdk.internal.org.objectweb.asm.ClassVisitor;
import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.FieldVisitor;
import jdk.internal.org.objectweb.asm.MethodVisitor;

import static org.objectweb.asm.Opcodes.*;

class json extends ClassVisitor {
    //cw用来复制
    ClassWriter cw = new ClassWriter(0);
    public json() {
        super(ASM4);
    }

    public void init(ClassWriter cw){
        this.cw = cw;
    }
    @Override
    public void visit(int version, int access, String className, String signature, String superName, String[] interfaces){
        System.out.println(className);
        cw.visit(52,access,"obj",signature,superName,interfaces);
    }

    @Override
    public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) {
        cw.visitField(access,name,descriptor,signature,value);
        return null;
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
//        cw.visitMethod(access,name,descriptor,signature,exceptions);
        return null;
    }
    //到达class文件尾部，访问结束
    @Override
    public void visitEnd(){
        // 生成默认的构造方法
        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        // 生成构造方法的字节码指令
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        // 构造函数访问结束
        mv.visitEnd();
    }
}